<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="95%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="订单规格" prop="flex">
          <el-input
            v-model="queryParams.flex"
            placeholder="请输入订单规格"
            clearable
            @keyup.enter="handleQuery"
            class="!w-180px"
          />
        </el-form-item>

        <el-form-item label="长度" prop="len">
          <el-input-number
            v-model="queryParams.len"
            placeholder="请输入长度"
            clearable
            @keyup.enter="handleQuery"
            class="!w-180px"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
            type="success"
            :loading="exportLoading"
            plain
            @click="handleExportDeliverSummaryReport"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column label="订单编号" align="center" prop="orderId" />
        <el-table-column label="规格" align="center" prop="type" />
        <el-table-column label="订单规格" align="center" prop="flex" />
        <el-table-column label="长度" align="center" prop="len" />
        <el-table-column label="JR/INT/SR" align="center" prop="jis" />
        <el-table-column label="转出部门" align="center" prop="outDeptName" />
        <el-table-column label="接收部门" align="center" prop="recvDeptName" />
        <el-table-column label="数量" align="center" prop="cnt" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="dialogVisible = false">关  闭</el-button>
    </template>
  </Dialog>

</template>
<script setup lang="ts">

import { dateFormatter } from '@/utils/formatTime'
import { DeliveryReceiptDetailApi, DeliveryReceiptDetailVO } from '@/api/hcxly/deliveryreceiptdetail'
import {DeliveryReceiptApi} from "@/api/hcxly/deliveryreceipt";
import download from "@/utils/download";

/** 产品拨交单 表单 */
defineOptions({ name: 'DeliveryReceiptSummaryReport' })


const loading = ref(true) // 列表的加载中
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const exportLoading = ref(false) // 导出的加载中


const list = ref<DeliveryReceiptDetailVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deliveryReceiptId: undefined,
  orderId: undefined,
  internalCode: undefined,
  productionCode: undefined,
  type: undefined,
  lastCollectionTime: [],
  outDeptId: undefined,
  outDeptName: undefined,
  recvDeptId: undefined,
  recvDeptName: undefined,
  submitTime: [],
  remark: undefined,
  createTime: [],
  flex : undefined,
  len: undefined,
  submitDeptName: undefined
})
const queryFormRef = ref() // 搜索的表单


/** 打开弹窗 */
const open = async (row?: any, params?: any) => {
  queryParams.orderId = row.orderId;
  queryParams.submitDeptName = row.remark;
  queryParams.outDeptName = row.outDeptName;
  queryParams.recvDeptName = row.recvDeptName;
  queryParams.submitTime = params.submitTime;
  await getList();
  dialogVisible.value = true
  dialogTitle.value = '提交单汇总'
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DeliveryReceiptDetailApi.getDeliveryReceiptSummaryReport(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}


/** 搜索按钮操作 */
const handleQuery = async () => {
  queryParams.pageNo = 1
  await getList()
}


/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleExportDeliverSummaryReport = async () => {
  const data = await DeliveryReceiptDetailApi.exportDeliverSummaryReport(queryParams)
  download.excel(data, '拨交综合报表-汇总.xls')
}

</script>
